﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Product;
using Broker.View.Comm;
using BrokerBase.Lib.Tool;

namespace Broker.View.ProductView
{
    /// <summary>
    /// BasicAmountForm.xaml 的交互逻辑
    /// </summary>
    public partial class BasicAmountForm 
    {
        private IBasicAmount ba;
        private IBasicAmount original;

        void BasicAmountForm_PreviewClosed(object sender, Telerik.Windows.Controls.WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                {
                    BaseTools.CopyTo<IBasicAmount>(ba, original);
                    if (CallBack != null)
                        CallBack(original);
                }
                else
                {
                    e.Cancel = true;
                }

            }
        }

        public BasicAmountForm(IBasicAmount basicAmt)
        {
            InitializeComponent();

            original = basicAmt;

            ba = new BasicAmount(original.Owner);

            BaseTools.CopyTo<IBasicAmount>(original, ba);

            Loaded += new RoutedEventHandler(BasicAmountForm_Loaded);
            PreviewClosed += new EventHandler<Telerik.Windows.Controls.WindowPreviewClosedEventArgs>(BasicAmountForm_PreviewClosed);
            
        }

        void BasicAmountForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = ba;
        }

        public bool IsCommit { get; set; }

        public Action<IBasicAmount> CallBack { get; set; }

        public IBasicAmount BasicAmount { get { return ba; } }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();

        }

        private bool Verify()
        {
            if (ba.Basic <= 0)
            {
                MessageBox.Show("基本金额不能等于0", "提示");
                return false;
            }

            if (ba.Min < ba.Basic)
            {
                MessageBox.Show("最小金额不能小于基本金额", "提示");
                return false;
            }

            if (ba.Max!=0 && ba.Max < ba.Basic)
            {
                MessageBox.Show("最大金额不能小于基本金额", "提示");
                return false;
            }


            return true;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }

    }
}
